#include <iostream>
#include <cmath>

using namespace std;

//#define DEBUG

#define W 7
#define H 8

bool chess_bak[W-2][H-2];
bool chess[W][H];
bool action[W-2][H-2];

void PrintChess() {
    cout << ">==========>" << endl;
    for (int i = 1; i < W-1; ++i) {
        for (int j = 1; j < H-1; ++j) {
            cout << chess[i][j] << " ";
        }
        cout << endl;
    }
    cout << "<==========<" << endl;
}

void Recovery() {
    for (int i = 1; i < W-1; ++i) {
        for (int j = 1; j < H-1; ++j) {
            chess[i][j] = chess_bak[i-1][j-1];
            action[i-1][j-1] = 0;
        }
    }
#ifdef DEBUG
    PrintChess();
#endif

}

void Next(int n) {
    chess[0][1] = (n&0x20) != 0;
    chess[0][2] = (n&0x10) != 0;
    chess[0][3] = (n&0x08) != 0;
    chess[0][4] = (n&0x04) != 0;
    chess[0][5] = (n&0x02) != 0;
    chess[0][6] = (n&0x01) != 0;
}

void FlipOne(int x, int y) {
    chess[x][y] = !chess[x][y];
    chess[x-1][y] = !chess[x-1][y];
    chess[x+1][y] = !chess[x+1][y];
    chess[x][y-1] = !chess[x][y-1];
    chess[x][y+1] = !chess[x][y+1];
}


bool Flip() {
    for (int w = 1; w < W-1; ++w) {
        for (int h = 1; h < H-1; ++h) {
            if (chess[w-1][h] == true) {
                FlipOne(w, h);
                action[w-1][h-1] = true;
            }
#if 0
            cout << ".........>>" << endl;
            for (int i = 0; i < W-1; ++i) {
                for (int j = 1; j < H-1; ++j) {
                    cout << action[i][j-1] << " ";
                }
                cout <<  endl;
            }
            cout << "........<<" << endl;
#endif
#if defined(DEBUG)
            PrintChess();
#endif
        }
    }
    for (int i = 1; i < H-1; ++i) {//最后一行
        if (chess[W-2][i] == true)
            return false;
    }
    return true;
}

void Process() {

    for (int n = 0; n < 64; ++n) {
#if defined(DEBUG)
        cout << "Next: " << n << endl;
#endif
        Next(n);
        Recovery();
        if (Flip()) {
#if defined(DEBUG)
            cout << "成功\n" << endl;
#endif
            break;
        }
    }
    for (int i = 0; i < W-2; ++i) {
        for (int j = 0; j < H-2; ++j) {
            cout << action[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {

    int N;
    cin >> N;

    for (int n = 1; n <= N; ++n) {
        for (int i = 0; i < W-2; i++) {
            for (int j = 0; j < H-2; ++j) {
                cin >> chess_bak[i][j];
            }
        }
        cout << "PUZZLE #" << n << endl;
        Process();
    }


    return 0;
}
